package me.fung.rem.app.settings

import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch
import me.fung.keen.app.view.View
import me.fung.rem.app.auth.PAGE_MODIFY_PASSWORD
import me.fung.rem.app.common.goBackButton
import me.fung.rem.app.navigation
import me.fung.rem.bean.repo.configAutoLoginDate
import me.fung.rem.bean.repo.configSearchDays
import me.fung.rem.bean.repo.configUseAddCard

const val PAGE_SETTINGS = "PAGE_SETTINGS"

class SettingsView : View<SettingsPresenter>() {

    @Composable
    override fun content(modifier: Modifier) {
        DisposableEffect(Unit) {
            p.receiveJob()
            onDispose {
                p.cancelJob()
            }
        }
        Box(modifier = modifier) {
            settingContent(Modifier.padding(20.dp))
            goBackButton()
        }
    }

    @Composable
    fun settingContent(modifier: Modifier) {
        Column(modifier = modifier) {
            setAutoLoginDate(Modifier)
            setSearchDays(Modifier)
            setAddCard(Modifier)
            setPassword(Modifier)
        }
    }
}

@Composable
fun setAddCard(modifier: Modifier.Companion) {
    var useCard by remember { mutableStateOf(configUseAddCard) }
    Row(modifier = modifier, verticalAlignment = Alignment.CenterVertically) {
        Text("添加记录使用：")
        Text("按钮")
        Switch(
            checked = useCard,
            onCheckedChange = {
                configUseAddCard = it
                useCard = it
            },
            colors = SwitchDefaults.colors(
                checkedThumbColor = MaterialTheme.colors.secondary,
                checkedTrackColor = MaterialTheme.colors.onSurface,
                checkedTrackAlpha = 0.5f,
                uncheckedThumbColor = MaterialTheme.colors.secondary,
                uncheckedTrackColor = MaterialTheme.colors.onSurface,
                uncheckedTrackAlpha = 0.5f,
            )
        )
        Text("卡片")
    }
}

@Composable
fun SettingsView.setSearchDays(modifier: Modifier) {
    val scope = rememberCoroutineScope { p.single }
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = modifier,
    ) {
        var searchDays by remember { mutableStateOf(configSearchDays.toFloat()) }
        Text("搜索记录${searchDays.toInt()}天")
        Slider(
            value = searchDays,
            valueRange = 0f..365f,
            onValueChange = {
                val v = it
                if (searchDays.toInt() != v.toInt()) {
                    scope.launch { p.saveSearchDays(v.toInt()) }
                }
                searchDays = it
            },
        )
    }
}

@Composable
fun SettingsView.setAutoLoginDate(modifier: Modifier) {
    val scope = rememberCoroutineScope { p.single }
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = modifier,
    ) {
        var progress by remember { mutableStateOf(configAutoLoginDate.toFloat()) }
        Text("自动登录天数${progress.toInt()}天")
        Slider(
            value = progress,
            valueRange = 0f..9f,
            onValueChange = {
                val v = it
                if (progress.toInt() != v.toInt()) {
                    scope.launch { p.saveAutoLoginDate(v.toInt()) }
                }
                progress = it
            },
        )
    }
}

@Composable
fun setPassword(modifier: Modifier) {
    Row(modifier = modifier) {
        Button(
            onClick = {
                navigation.goto(PAGE_MODIFY_PASSWORD)
            }
        ) {
            Text("修改密码")
        }
    }
}
